package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiT_log;
import cn.com.api.service.ApiSmslogService;
import cn.com.api.service.ApiT_logService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "log/")
public class ApiT_logController {
	@Autowired
	private ApiT_logService apiT_logService;
	@Autowired
	private ApiSmslogService apiSmslogService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger.getLogger(ApiT_logController.class);

	/**
	 * 新增 添加收藏
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "insertT_log")
	public void insertT_log(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String c_uid = request.getParameter("c_uid");
		String token = request.getParameter("token");
		String c_userid = request.getParameter("c_userid");
		String c_act = request.getParameter("c_act");
		String c_type = request.getParameter("c_type");
		String cityid = request.getParameter("cityid");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (checkService.checkToken(c_userid, token, map)) {
				ApiT_log t_log = new ApiT_log();
				t_log.setC_uid(Long.valueOf(c_uid));
				t_log.setC_userid(c_userid);
				t_log.setC_type(Integer.valueOf(c_type));
				t_log.setC_act(c_act);
				// t_log.setCityid(cityid);
				// c_act || c_g(商家关注) c_d(优惠劵关注) c_good(商品关注)
				// c_type || 0 商家 1 劵, 2 商品
				t_log.setC_appdate(new Date());

				apiT_logService.addOrCancelLog(t_log, map);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_logById")
	public void findT_logById(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String c_id = request.getParameter("c_id");
		ApiT_log t_log = apiT_logService.findT_logById(Integer.valueOf(c_id));

		// response
		response.setCharacterEncoding("UTF-8");
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "查询成功！");
		map.put("datas", t_log);
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据用户id查询收藏记录
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_logsByUserId")
	public void findT_logsByUserId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String c_userid = request.getParameter("c_userid");
			String token = request.getParameter("token");
			if (checkService.checkToken(c_userid, token, map)) {
				List<ApiT_log> list = apiT_logService.findT_logsByUserId(c_userid);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
				map.put("datas", list);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		// response
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据用户id查询收藏商铺
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_logs4ShopsByUserId")
	public void findT_logs4ShopsByUserId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String c_userid = request.getParameter("c_userid");
		String token = request.getParameter("token");
		String cityid = request.getParameter("cityid");
		try {
			if (checkService.checkToken(c_userid, token, map)) {
				apiT_logService.findT_logs4ShopsByUserId(c_userid, cityid, map);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据用户id查询收藏的商品
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_logs4GoodsByUserId_1")
	public void findT_logs4GoodsByUserId_1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String c_userid = request.getParameter("c_userid");
		String token = request.getParameter("token");
		String cityid = request.getParameter("cityid");

		try {
			if (checkService.checkToken(c_userid, token, map)) {
				apiT_logService.findT_logs4GoodsByUserId_1(c_userid, cityid, map);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据用户id查询收藏商铺
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_logs4ShopsByUserId_1")
	public void findT_logs4ShopsByUserId_1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String c_userid = request.getParameter("c_userid");
		String token = request.getParameter("token");
		String cityid = request.getParameter("cityid");
		try {
			if (checkService.checkToken(c_userid, token, map)) {
				apiT_logService.findT_logs4ShopsByUserId_1(c_userid, cityid, map);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据用户id查询收藏的商品
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_logs4GoodsByUserId")
	public void findT_logs4GoodsByUserId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String c_userid = request.getParameter("c_userid");
		String token = request.getParameter("token");
		String cityid = request.getParameter("cityid");

		try {
			if (checkService.checkToken(c_userid, token, map)) {
				apiT_logService.findT_logs4GoodsByUserId(c_userid, map);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 取消收藏
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "cancelT_log")
	public void cancelT_log(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String c_uid = request.getParameter("c_uid");
		String token = request.getParameter("token");
		String c_userid = request.getParameter("c_userid");
		String c_act = request.getParameter("c_act");
		String c_type = request.getParameter("c_type");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (checkService.checkToken(c_userid, token, map)) {
				// c_act || c_g(商家关注) c_d(优惠劵关注) c_good(商品关注)
				// c_type || 0 商家 1 劵, 2 商品

				apiT_logService.deleteT_logByUidUseridActType(Integer.parseInt(c_uid), c_userid, c_act, c_type);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "取消收藏成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
